define(function(require, exports){
	var pubjs	= require('pubjs');
	var popwin	= require('@base/dialog').base;
	var grid    = require('@base/grid').baseNoDate;
	var $ = require('jquery');
	var labels = require('@sys_labels').labels;
	var util = require('util');
	var Base = require('@base/common/base');
	var selectOnTab = require('modules/selectOnTab');

	var Grid = grid.extend({
		init: function(config){
			config = pubjs.conf(config, {
				cols: [
					{name:"_id", text: 'ID', width: 50, type:'fixed',sort:true,align:'center'},
					{name: 'Name', text: LANG('订单名称'), render:'renderName',width:200,align:'left'},
					{name: 'Time', text: LANG('订单时间'), type:'index',render:'renderDealTime',width:200},
					{name: 'Price', text: LANG('订单价格'), type:'index',render:'renderPrice',width:200},
					{name: 'Status', text:LANG('状态'), render:'renderStatus', align:'center'},
					{name: 'channelName', text:LANG('渠道'), align:'center'},
					{name: 'SpotsInfo', text:LANG('广告位数量'), render:'renderCount',align:'center'},
					{name: 'UpdateTime', text:LANG('更新时间'), render: 'renderTime',align:'center'}
				]
				,sort: 'CreateTime'
				,reqType: 'websocket'
				,opClick: true
				,hasRefresh: true
				,hasTab: false
				,hasMenu: false
				,hasPager: true
				,hasSearch: true
				,hasExport: false
				,hasAmount: false
				,auto_load: true
				,hasSelect: true
				,eventDataLoad: true
				,param: {
					limit: 8
				}
				,pager: {
					size: 8
					,showSizeTypes: 0
					,showJumper: 0
				}
				,'list':{
					'rowSelect': true,
					'scroll_type': 'row',
					'scroll_size': 8
				}
				,'url': 'deal/list'
			});

			this.Super('init', arguments);
		}
		,renderPrice: function(i, val, data) {
			var price = data.DealInfo && data.DealInfo.Price || 0;
			return labels.formatCurrencyKeep2(price);
		}
		,renderDealTime: function(i, val, data) {
			var info;
			var ret = '';
			var begin, end, beginKey;

			// // 需要手动设置订单开启时间
			// if(~needSetSpot.indexOf(data.AdxId))
			// {
			// 	info = data && data.ManualInfo;
			// 	beginKey = 'StartTime';
			// }
			// // 渠道同步过来的开启结束时间
			// else
			// {
				info = data && data.DealInfo;
				beginKey = 'BeginTime';
			// }

			if(info)
			{
				begin = info[beginKey] ? util.date("Y-m-d", info[beginKey]) : null;
				end = info.EndTime ? util.date("Y-m-d", info.EndTime) : null;
			}

			ret += begin ? begin : LANG('未知');
			ret += ' - ' + (end ? end : LANG('不限'));
			return !begin && !end ? LANG('不限') : ret;

		}
		,renderTime: function(i, val) {
			return val ? util.date("Y-m-d H:i:s", val) : null;
		}
		,renderStatus: function(i,val,data,con){
			var text;
			switch (+val) {
				case 1:
					text = LANG('开启');
					break;
				case 2:
					text = LANG('关闭');
					break;
				default :
					text = '-';
			}
			return text;
		}
		,renderName: function(i, val, data, con){
			val = val || LANG('-');
			return $('<div class="M-tableListWidthLimit"/>')
				.attr({
					'title': val,
					// 'target': '_blank',
					// 'href': '#admin/deal/edit/'+ data._id + '?Type=' + this.parent().getConfig('Type')
				})
				.text(val).width(con.width);
		}
		,renderCount: function(idx, val, row, col){
			return (val && val.length || 0).toString();
		}
		,onChangeSelect: function(ev){
			var data = ev.param;
			this.fire('gridChangeSelect', data);
		}
	});

	exports.grid = Grid;

	var Main = selectOnTab.pop.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'title': LANG('选择订单'),
				'width': 1000,
				'showFoot': true,
				'param': null,
				'class': 'P-campaign-select',
				'silence': false,
				'size': 'm',
				'height': 650,
				'selectEvent': 'selectOrderSuccess',
				// 'buttons': ['cancel', null]

				'main_config': {
					'url': 'deal/list',
					'param': null,
					'gridKey': '_id',
					'tab': {
						'all': {
							'text': LANG('全部'),
							'uri': 'campaign/popwin/selectOrder.grid',
							'config': {}
						},
						'select': {
							'text': LANG('已选'),
							'uri': 'campaign/popwin/selectOrder.grid',
							'config': {}
						}
					}
				}
			});

			this.Super('init', arguments);
		}
	});

	exports.main = Main;

});